//
// Description: 6013. 合并零之间的节点
// Created by Loading on 2022/2/20.
//

#include <bits/stdc++.h>
#include <Data_Structure_Definition.h>
#include <Utils.h>

using namespace std;

ListNode *mergeNodes(ListNode *head) {
    int sum = 0;
    auto res = head;
    for (auto node = head->next; node; node = node->next) {
        if (node->val != 0) {
            sum += node->val;
        } else {
            res->next->val = sum;
            res = res->next;
            sum = 0;
        }
    }
    res->next = nullptr;

    return head->next;
}

int main() {
    vector<int> nums = {0, 1, 0, 3, 0, 2, 2, 0};
    auto head = generateListNode(nums);
    auto res = mergeNodes(head);
    while (res) {
        cout << res->val << " ";
        res = res->next;
    }

    return 0;
}